package com.meiqiongproperty.yiguanjia.fragment.user;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.meiqiongproperty.yiguanjia.R;
import com.meiqiongproperty.yiguanjia.databinding.TenmentListItemBinding;
import com.meiqiongproperty.yiguanjia.fragment.home.adapter.OnItemClickListener;
import com.meiqiongproperty.yiguanjia.httpbean.HouseBean;
import com.meiqiongproperty.yiguanjia.utils.LogUtil;

import java.util.List;

/**
 * 小区适配器
 * @version V1.0
 * @packge com.meiqiongproperty.yiguanjia.fragment.user
 * @User ChenJing
 * @Date 2017/5/11
 */

public class TenmentAdapter extends RecyclerView.Adapter<TenmentAdapter.BindingHolder> {
        Context context;
    List<HouseBean.House> informationBean;
    private OnItemClickListener mOnItemClickListener;

    public TenmentAdapter(Context context, List<HouseBean.House> informationBean) {
            this.context = context;
            this.informationBean = informationBean;
    }

    @Override
    public BindingHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            TenmentListItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.tenment_list_item, parent, false);
            BindingHolder holder = new BindingHolder(binding.getRoot());
            holder.setBinding(binding);
             return holder;
    }

    @Override
    public void onBindViewHolder(final BindingHolder holder, int position) {
        HouseBean.House bean = informationBean.get(position);
        LogUtil.d("bean="+bean.toString());
        holder.binding.house.setText(bean.getHousesName());
        holder.binding.executePendingBindings();
        if (mOnItemClickListener != null) {
            /**
             * 这里加了判断，itemViewHolder.itemView.hasOnClickListeners()
             * 目的是减少对象的创建，如果已经为view设置了click监听事件,就不用重复设置了
             * 不然每次调用onBindViewHolder方法，都会创建一个监听事件对象，增加了内存的开销
             */
            if (mOnItemClickListener != null) {
                /**
                 * 这里加了判断，itemViewHolder.itemView.hasOnClickListeners()
                 * 目的是减少对象的创建，如果已经为view设置了click监听事件,就不用重复设置了
                 * 不然每次调用onBindViewHolder方法，都会创建一个监听事件对象，增加了内存的开销
                 */
                if (!holder.itemView.hasOnClickListeners()) {
                    Log.e("ListAdapter", "setOnClickListener");
                    holder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            int position = holder.getPosition();
                            mOnItemClickListener.onItemClick(v, position);
                        }
                    });
                }
            }
        }
    }
    @Override
    public int getItemCount() {
        return informationBean.size();
    }
    public class BindingHolder extends RecyclerView.ViewHolder {
        TenmentListItemBinding binding;

        public TenmentListItemBinding getBinding() {
            return binding;
        }

        public void setBinding(TenmentListItemBinding binding) {
            this.binding = binding;
        }

        public BindingHolder(View itemView) {
            super(itemView);
        }
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }
}

